﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using System;
using Microsoft.Xna.Framework.Graphics;
using AAEngine.Engine;
using AAEngine.Engine.Entities;
using AAEngine.Engine.Resources;

namespace AAEngine.Rendering2D.Fonts
{
    public class SpriteFontComponent : EntityComponent
    {
        private string _fontFilename;
        private FontResource _font;

        public string FontFilename
        {
            get { return _fontFilename; }
            set
            {
                if (_fontFilename != value)
                {
                    if (_font != null)
                    {
                        AAGame.ResourceManager.Unload(_font.Filename);

                        _font = null;
                    }
                    _fontFilename = value;
                    _font = (FontResource)AAGame.ResourceManager.Load(value, typeof(FontResource));
                }
            }
        }

        public FontResource Font
        {
            get { return _font; }
            set
            {
                _font = value;
                _fontFilename = _font.Filename;
            }
        }

        public SpriteFont FontData
        {
            get { return _font.Font; }
        }

        public override void OnAdd()
        {
            base.OnAdd();

            if (_font == null && !String.IsNullOrEmpty(_fontFilename))
            {
                _font = (FontResource)AAGame.ResourceManager.Load(_fontFilename, typeof(FontResource));
            }
        }

        public override void OnRemove()
        {
            if (_font != null)
            {
                AAGame.ResourceManager.Unload(_font.Filename);
                _font = null;
            }
            base.OnRemove();
        }
    }
}
